package com.naiterui.ehp.bs.b2c.product.controller.inner;

import com.naiterui.ehp.bp.bo.b2c.SkuUsageBO;
import com.naiterui.ehp.bp.bo.b2c.cms.CmsSkuBO;
import com.naiterui.ehp.bp.bo.b2c.cms.ProductBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bs.b2c.product.exception.ProductBusinessException;
import com.naiterui.ehp.bs.b2c.product.service.ISkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.Set;

@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG, SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@Validated
@RequestMapping("b2c/inner/cms/sku")
@RestController
public class InnerSkuController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InnerSkuController.class);

    @Autowired
    private ISkuService skuService;

    /**
     * 更新保存sku
     *
     * @param operator
     * @param sku
     * @return
     */
    @ApiOperation(value = "更新保存sku")
    @PostMapping
    public ResponseVO<Void> saveOrUpdate(@RequestHeaderDecode @NotEmpty(message = "操作用户不能为null") String operator,
                                         @RequestBody CmsSkuBO sku) throws ProductBusinessException {
        LOGGER.info("保存更新sku 请求参数：operator = {}, sku = {}", operator, sku);
        this.skuService.saveOrUpdate(operator, sku, sku.getThirdId());
        return PublicService.returnResponseVO(null);
    }

    /**
     * 保存或更新商品信息(product + sku)
     *
     * @param operator
     * @param product
     * @return
     * @throws ProductBusinessException
     */
    @ApiOperation(value = "保存或更新商品信息(product + sku)")
    @PostMapping("saveOrUpdate")
    public ResponseVO<Void> saveOrUpdate(@RequestHeaderDecode @NotEmpty(message = "操作用户不能为null") String operator,
                                         @RequestBody ProductBO product) throws ProductBusinessException {
        LOGGER.info("保存更新商品 请求参数：operator = {}, product = {}", operator, product);
        this.skuService.saveOrUpdateProduct(operator, product);
        return PublicService.returnResponseVO(null);
    }


    /**
     * 将某个sku设置成默认
     *
     * @param productId
     * @param skuId
     * @param operator
     * @return
     */
    @ApiOperation(value = "将某个sku设置成默认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "药品ID", paramType = "query", dataType = "long", required = true),
            @ApiImplicitParam(name = "skuId", value = "SKUID", paramType = "query", dataType = "long", required = true)})
    @PutMapping("defautl/{productId}/{skuId}")
    public ResponseVO<Void> updateDefault(@PathVariable("productId") @NotNull(message = "productId不能为null") Long productId,
                                          @PathVariable("skuId") @NotNull(message = "skuId不能为null") Long skuId,
                                          @RequestHeaderDecode String operator) {
        LOGGER.info("设置默认sku 请求参数：productId = {}, skuId = {}, operator = {}", productId, skuId, operator);
        this.skuService.updateDefault(productId, skuId, operator);
        return PublicService.returnResponseVO(null);
    }

    /**
     * 批量获取sku价格
     *
     * @param skuIdSet
     * @return
     */
    @ApiOperation(value = "批量获取sku价格")
    @ApiImplicitParams({@ApiImplicitParam(name = "skuIdSet", value = "药品SKUID集合", paramType = "body",
            dataType = "array", required = true)})
    @PostMapping("/getSkuPrice")
    public ResponseVO<Map<Long, Integer>> getSkuPrice(@RequestBody Set<Long> skuIdSet) {
        return PublicService.returnResponseVO(this.skuService.getSkuPrice(skuIdSet));
    }

    /**
     * 修改该sku的默认用法用量和安全用量
     *
     * @param skuUsageBO
     * @return
     */
    @ApiOperation(value = "修改该sku的默认用法用量和安全用量")
    @PostMapping("/updateSkuUsage")
    public ResponseVO<Void> updateSkuUsage(@RequestBody SkuUsageBO skuUsageBO) throws BusinessException {
        this.skuService.updateSkuUsage(skuUsageBO);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }
}
